// selfpool.h
#ifndef __SELFPOOL__H
#define __SELFPOOL__H

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

typedef struct threadpool_task
{
    void (*function)(void *);
    void *argument;
    void (*callback)(void *);
    int priority;
    struct threadpool_task *next;
} threadpool_task_t;

typedef struct threadpool
{
    pthread_mutex_t mutex;         // 互斥锁
    pthread_cond_t cond;           // 条件变量
    pthread_t *threads;            // 线程数组
    threadpool_task_t *queue_head; // 任务队列头
    threadpool_task_t *queue_tail; // 任务队列尾

    int thread_count;        // 当前线程数
    int max_thread_count;    // 最大线程数
    int min_thread_count;    // 最小线程数
    int shutdown;            // 关闭标志
    int task_count;          // 待处理任务数
    int idle_thread_count;   // 空闲线程数
    int active_thread_count; // 活跃线程数
} threadpool_t;

typedef struct threadpool_status
{
    int thread_count;      // 当前线程数
    int task_count;        // 任务数
    int idle_thread_count; // 空闲线程数
} threadpool_status_t;

// 接口声明
void *worker(void *arg);
threadpool_t *threadpool_init(int min_threads, int max_threads);
int threadpool_get_status(threadpool_t *pool, threadpool_status_t *status);
void threadpool_add_task(threadpool_t *pool, void (*task_func)(void *),
                         void *arg, void (*callback)(void *), int priority);
int threadpool_adjust(threadpool_t *pool, int min_threads, int max_threads);
void threadpool_destroy(threadpool_t *pool);

#endif